package es.unileon.prg2.censo.handler;

/**
 * @author Manuel Molinos Crespo, Adrian Campazas Vega, Alberto Castro Delgado, Pedro Alvarez Alvarez, Adrian Cabello Galvan, Jose Alvaro Fernandez Alvarez
 *
 */
import org.apache.log4j.Logger;
/**
 * @author Manuel Molinos Crespo
 * @author Adrian Campazas Vega
 * @author Alberto Castro Delgado
 * @author Pedro Alvarez Alvarez
 * @author Adrian Cabello Galvan
 * @author Jose Alvaro Fernandez Alvarez
 *
 */

import es.unileon.prg2.censo.exception.CensoException;
import org.apache.log4j.PropertyConfigurator;
public class ShortName implements Handler{
	static Logger logger = Logger.getLogger(ShortName.class.getName());
	private String id;
	
	/**
	 * Constructor used for created the ID of the components.
	 * @param id String composite for 2 letters
	 * @throws CensoException
	 */
	public ShortName (String id) throws CensoException {
		StringBuffer message = new StringBuffer();
		PropertyConfigurator.configure("etc/log4j.properties");
		if (id.length() > 2){
			message.append("The length of the identifier should be less than 2. "+id+" is not valid.\n");
			logger.error("The length of the identifier should be less than 2\n");
		}else if(id.length()==0){
			message.append("The identifier can not be an empty one.\n");
			logger.error("The identifier can not be an empty one.\n");
		}else if((id.length()==1) && (Character.isDigit(id.charAt(0)))){
			message.append("The identifier can only include letters. "+id+" is not valid.\n");
			logger.error("The identifier can only include letters.");
		}else if ((id.length()==2)&&((Character.isDigit(id.charAt(0))||(Character.isDigit(id.charAt(1)))))){
			message.append("the identifier can only include letters. "+id+" is not valid.\n");
			logger.error("The identifier can only include letters.");
		}else{
			this.id=id;
			logger.info(id+" created successfully");
		}
		
		if(message.length()!=0){
			throw new CensoException(message.toString());
		}
	}
	
	/**
	 * Other constructor used for created the ID of the components from a Handler parameter.
	 * @param id String composite for 2 letters
	 * @throws CensoException
	 */
	public ShortName(Handler id) throws CensoException {
		this(id.toString());
	}

	@Override
	public int compareTo(Handler another) {
		return this.id.compareTo(another.toString());
	}

	@Override
	public String toString()
	{
		return id;
	}
	
}
